package javaNIO1.Selectors;


import javaNIO1.Count;
import org.bouncycastle.tsp.TSPUtil;
import org.springframework.util.CollectionUtils;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


import static javaNIO1.Count.*;

/**
 * Created with Intel IDEA.
 *
 * @Auther: gjt
 * @Date: 2024/10/22 21:53
 */
public class FileLockingThread extends Thread {


    public FileLockingThread(){ThreadIds++;}

    @Override
    public void run() {
        synchronized (Count.class){
            if (FileIsdLock == 0) {
                try {
                    in = new FileInputStream(path);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
                fileChannel = in.getChannel();

                // 尝试获取文件锁
                try {
                    lock = fileChannel.tryLock(0, 100, true);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "获取锁");
                FileIsdLock++;
            }
        }

        try {
                System.out.println("任务还有" + ThreadIds + "个任务");
                System.out.println("Thread " + Thread.currentThread().getName() + " acquired the lock on " + Count.path);

                // 模拟文件读取操作
                try {
                    System.out.println(Thread.currentThread().getName() + "操作");
                    Thread.sleep(10000); // 模拟耗时操作


//                    if (threadIds == 1) {
//                        System.out.println("线程" + Thread.currentThread().getName() + "正在休息");
//                        Thread.sleep(5000);
//                        System.out.println("线程" + Thread.currentThread().getName() + "熟睡5秒");
//                        // 模拟耗时操作
//                    }


                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }

                System.out.println("Thread " + Thread.currentThread().getName() + "结束工作");
                ThreadIds--;

                System.out.println( Thread.currentThread().getName() + "还有" + ThreadIds);
                synchronized ( Count.class) {
                    if (ThreadIds == 0 && FileIsdLock == 1) {
                        System.out.println(Thread.currentThread().getName() + "任务开始释放锁");
                        // 释放文件锁
                        lock.release();
                        fileChannel.close();
                        in.close();
                        System.out.println(Thread.currentThread().getName() + "任务结束释放锁1");
                        FileIsdLock--;
                    }
                }

        } catch (IOException e) {
            System.err.println("Error accessing file: " + path);
            e.printStackTrace();
        }
//        finally {
//            if (ThreadIds == 0 && FileIsdLock == 1) {
//                // 释放文件锁
//                if(lock != null){
//
//                    try {
//                        lock.release();
//                    } catch (IOException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//
//                if(fileChannel != null){
//
//                    try {
//                        fileChannel.close();
//                    } catch (IOException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//
//                if(in != null){
//                    try {
//                        in.close();
//                    } catch (IOException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//                System.out.println( Thread.currentThread().getName() + "任务结束释放锁2");
//            }
//        }
    }

}
